package com.woniu.controller;


import com.woniu.commons.Result;
import com.woniu.model.EArtical;
import com.woniu.service.IEArticalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author Kevin Kwok
 * @since 2021-04-16
 */
@RestController
@RequestMapping("/artical")
public class EArticalController {

    @Resource
    private IEArticalService articalService;

    /*新增文章*/
    @RequiresPermissions({"文章发表"})
    @GetMapping("/pulishNew")
    public Result addNewArtical(String title,String content,Integer typeId){
        int i = articalService.addNewArtical(title, content, typeId);
        if(i==0){
            return new Result("400","发布失败",null,null);

        }
        return new Result("200","发布成功",null,null);
    }

    /*修改文章*/
    @RequiresPermissions({"文章发表"})
    @GetMapping("/edit")
    public Result editNewArtical(String title,String content,Integer typeId,Integer id){
        int i = articalService.editNewArtical(title, content, typeId, id);
        if(i==0){
            return new Result("400","修改失败",null,null);

        }
        return new Result("200","修改成功",null,null);
    }
    /*删除文章*/
    @RequiresPermissions({"文章发表"})
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        int i = articalService.deleteArtical(id);
        if(i==0) {
            return new Result("400", "删除失败", null, null);
        }
        return new Result("200","删除成功",null,null);
    }
    /*查询所有文章*/
    @RequiresPermissions({"文章发表"})
    @GetMapping("/queryAll")
    public Result queryAll(){
        List<EArtical> eArticals = articalService.queryAll();
        if(eArticals==null) {
            return new Result("400", "fail", null, null);
        }
        return new Result("200","success",null,eArticals);
    }
}

